import React, { ComponentType, forwardRef } from 'react';

// eslint-disable-next-line @typescript-eslint/ban-types
type AnyObject = {};

export type SetupPageProps<Props extends AnyObject> = {
  render: ComponentType<Props>,
} & Props

export function setupPage<Options extends AnyObject, Props extends AnyObject>(
  usePageInit: (opts?: Options) => Props,
  PageComponent: ComponentType<SetupPageProps<Props>>,
) {
  return (render: ComponentType<Props>, opts?: Options) => {
    return forwardRef<ComponentType<SetupPageProps<Props>>>(function ForwardPage(_, ref) {
      const props = usePageInit(opts);

      return <PageComponent {...props} render={render} ref={ref}/>;
    });
  };
}
